package com.Suyin.pma.logging;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class ApplicationLoggerAspect {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut("within(com.Suyin.pma.controller..*)"+"|| within(com.Suyin.pma.dao..*)")
    public void definePackagePointcuts(){
        //空方法，只是用来定义切点
    }

    /*
    @Before("definePackagePointcuts()")
    public void loggerBefore(JoinPoint joinPoint){
        logger.debug("\n\n\n");
        logger.debug("*********方法执行前*****************");
        logger.debug(
                "{}.{}(),输入参数{}",
                joinPoint.getSignature().getDeclaringTypeName(),
                joinPoint.getSignature().getName(),
                Arrays.toString(joinPoint.getArgs())
        );
        logger.debug("--------------------------------------------");
        logger.debug("\n\n\n");
    }

   @After("definePackagePointcuts()")
    public void loggerAfter(JoinPoint joinPoint){
        logger.debug("\n\n\n");
        logger.debug("*********方法执行前*****************");
        logger.debug(
                "{}.{}(),输入参数{}",
                joinPoint.getSignature().getDeclaringTypeName(),
                joinPoint.getSignature().getName(),
                Arrays.toString(joinPoint.getArgs())
        );
        logger.debug("---------------------------------------------");
        logger.debug("\n\n\n");
    }
    */

    @Around("definePackagePointcuts()")
    public Object logAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        logger.debug("\n\n\n");
        logger.debug("*********方法执行前*****************");
        logger.debug(
                "{}.{}(),输入参数{}",
                proceedingJoinPoint.getSignature().getDeclaringTypeName(),
                proceedingJoinPoint.getSignature().getName(),
                Arrays.toString(proceedingJoinPoint.getArgs())
        );
        logger.debug("---------------------------------------------");
        logger.debug("\n\n\n");

        Object object = proceedingJoinPoint.proceed();

        logger.debug("\n\n\n");
        logger.debug("*********方法执行后*****************");
        logger.debug(
                "{}.{}(),输入参数{}",
                proceedingJoinPoint.getSignature().getDeclaringTypeName(),
                proceedingJoinPoint.getSignature().getName(),
                Arrays.toString(proceedingJoinPoint.getArgs())
        );
        logger.debug("---------------------------------------------");
        logger.debug("\n\n\n");

        return object;

    }

}
